const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const queueController = require('../controllers/queue.controller');

const router = new Router({ prefix: '/api/v1/queues' });

/**
 * @swagger
 * /api/v1/queues:
 *   get:
 *     tags: [队列管理]
 *     summary: 获取所有队列
 *     description: 获取系统中所有队列的列表
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 queues:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       name:
 *                         type: string
 *                         example: "email"
 *                       status:
 *                         type: string
 *                         example: "active"
 *                       jobCount:
 *                         type: integer
 *                         example: 100
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission('queue:read'), queueController.getAllQueues);

/**
 * @swagger
 * /api/v1/queues/{queueName}/status:
 *   get:
 *     tags: [队列管理]
 *     summary: 获取队列状态
 *     description: 获取指定队列的详细状态信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: queueName
 *         required: true
 *         schema:
 *           type: string
 *         description: 队列名称
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 name:
 *                   type: string
 *                   example: "email"
 *                 status:
 *                   type: string
 *                   example: "active"
 *                 jobCount:
 *                   type: integer
 *                   example: 100
 *                 completedCount:
 *                   type: integer
 *                   example: 90
 *                 failedCount:
 *                   type: integer
 *                   example: 10
 *                 delayedCount:
 *                   type: integer
 *                   example: 5
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 队列不存在
 */
router.get('/:queueName/status', checkAuth, checkPermission('queue:read'), queueController.getQueueStatus);

/**
 * @swagger
 * /api/v1/queues/{queueName}/jobs:
 *   post:
 *     tags: [队列管理]
 *     summary: 添加任务
 *     description: 向指定队列添加新任务
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: queueName
 *         required: true
 *         schema:
 *           type: string
 *         description: 队列名称
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - data
 *             properties:
 *               data:
 *                 type: object
 *                 description: 任务数据
 *               options:
 *                 type: object
 *                 description: 任务选项
 *                 properties:
 *                   priority:
 *                     type: integer
 *                     description: 优先级（1-10）
 *                   delay:
 *                     type: integer
 *                     description: 延迟时间（毫秒）
 *                   attempts:
 *                     type: integer
 *                     description: 重试次数
 *     responses:
 *       201:
 *         description: 添加成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 jobId:
 *                   type: string
 *                   example: "job_001"
 *                 message:
 *                   type: string
 *                   example: "任务添加成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 队列不存在
 */
router.post('/:queueName/jobs', checkAuth, checkPermission('queue:write'), queueController.addJob);

/**
 * @swagger
 * /api/v1/queues/{queueName}/jobs/{jobId}:
 *   get:
 *     tags: [队列管理]
 *     summary: 获取任务详情
 *     description: 获取指定任务的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: queueName
 *         required: true
 *         schema:
 *           type: string
 *         description: 队列名称
 *       - in: path
 *         name: jobId
 *         required: true
 *         schema:
 *           type: string
 *         description: 任务ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "job_001"
 *                 data:
 *                   type: object
 *                   description: 任务数据
 *                 status:
 *                   type: string
 *                   example: "completed"
 *                 progress:
 *                   type: integer
 *                   example: 100
 *                 attempts:
 *                   type: integer
 *                   example: 1
 *                 createdAt:
 *                   type: string
 *                   format: date-time
 *                 processedAt:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 队列或任务不存在
 */
router.get('/:queueName/jobs/:jobId', checkAuth, checkPermission('queue:read'), queueController.getJob);

/**
 * @swagger
 * /api/v1/queues/{queueName}/jobs/{jobId}:
 *   delete:
 *     tags: [队列管理]
 *     summary: 删除任务
 *     description: 从队列中删除指定任务
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: queueName
 *         required: true
 *         schema:
 *           type: string
 *         description: 队列名称
 *       - in: path
 *         name: jobId
 *         required: true
 *         schema:
 *           type: string
 *         description: 任务ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "任务删除成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 队列或任务不存在
 */
router.delete('/:queueName/jobs/:jobId', checkAuth, checkPermission('queue:delete'), queueController.removeJob);

/**
 * @swagger
 * /api/v1/queues/{queueName}/pause:
 *   post:
 *     tags: [队列管理]
 *     summary: 暂停队列
 *     description: 暂停指定队列的任务处理
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: queueName
 *         required: true
 *         schema:
 *           type: string
 *         description: 队列名称
 *     responses:
 *       200:
 *         description: 暂停成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "队列已暂停"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 队列不存在
 */
router.post('/:queueName/pause', checkAuth, checkPermission('queue:write'), queueController.pauseQueue);

/**
 * @swagger
 * /api/v1/queues/{queueName}/resume:
 *   post:
 *     tags: [队列管理]
 *     summary: 恢复队列
 *     description: 恢复指定队列的任务处理
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: queueName
 *         required: true
 *         schema:
 *           type: string
 *         description: 队列名称
 *     responses:
 *       200:
 *         description: 恢复成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "队列已恢复"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 队列不存在
 */
router.post('/:queueName/resume', checkAuth, checkPermission('queue:write'), queueController.resumeQueue);

/**
 * @swagger
 * /api/v1/queues/{queueName}/clean:
 *   post:
 *     tags: [队列管理]
 *     summary: 清理队列
 *     description: 清理指定队列中的已完成或失败任务
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: queueName
 *         required: true
 *         schema:
 *           type: string
 *         description: 队列名称
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               grace:
 *                 type: integer
 *                 description: 保留时间（毫秒）
 *     responses:
 *       200:
 *         description: 清理成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "队列清理成功"
 *                 removed:
 *                   type: integer
 *                   example: 100
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 队列不存在
 */
router.post('/:queueName/clean', checkAuth, checkPermission('queue:write'), queueController.cleanQueue);

module.exports = router; 